package com.olympus.hermes.common.bean;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.persistence.AttributeConverter;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @author Mickey
 * @since 2021/2/6
 */
public class JsonArrayBeanConvert<T> implements AttributeConverter<List<T>, String> {

    private final ObjectMapper objectMapper;

    private final Class<T> xclazz;

    public JsonArrayBeanConvert(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
        this.xclazz = (Class<T>) (((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments())[0];
    }


    @Override
    public String convertToDatabaseColumn(List<T> attribute) {
        try {
            return objectMapper.writeValueAsString(attribute);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<T> convertToEntityAttribute(String dbData) {
        return null;
    }
}
